package com.naizhuai.ui.task.multtype;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.naizhuai.R;
import com.naizhuai.util.MPChartHelper;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author：lichenxi
 * @date 2018/5/9 15
 * email：525603977@qq.com
 * Fighting
 */
public class DataChartViewBinder extends ItemViewBinder<DataChart, DataChartViewBinder.ViewHolder> {

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_data_chart, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull DataChart dataChart) {
        holder.initCharView(dataChart);
        holder.title.setVisibility(View.GONE);
        holder.titleCount.setVisibility(View.GONE);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.linechart)
        LineChart lineChart;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.title_count)
        TextView titleCount;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


        private void initCharView(DataChart dataChart) {
            if (dataChart.data != null && dataChart.data.size() > 0) {
                ArrayList<String> xValues = new ArrayList<>();
                ArrayList<Float> yValues = new ArrayList<>();
                for (DataChart.DataBean dataBean : dataChart.data) {
                    xValues.add(dataBean.xPoint);
                    yValues.add(Float.parseFloat(dataBean.yPoint));
                }
                MPChartHelper.setLineChart(lineChart, xValues, yValues, "", false);
            }
        }
    }
}
